
var util = require('../../utils/util.js');

import { Data } from './temperatureone.data';


var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Data:Data,
    date: '',
    time: '',
    type: "",
    tiwen: "",
    out: {},
    dietId: '',
    temperature: '',
    motionlongtime:"",
    starttime: '',
    motiontime:'请输入运动开始时间',
    wytypeshow:3,
    yytypeshow:3,
    

  array: ['有氧运动', '无氧运动'],
  index:0,
  newarray:[],
  yy:'',
  wy:'',
  objectArray: [
    {
      id: 0,
      name: '有氧运动'
    },
    {
      id: 1,
      name: '无氧运动'
    }
  ],
 
  },
 
 //方法一
  //选择运动打卡类型（多选）  ----添加操作
  motiontype:function(e){
    if (e.detail.value == '0') {
      this.setData({
        yytypeshow: 0,//0 
      })
    } else if (e.detail.value == '1') {
      this.setData({
        wytypeshow: 1,//1
      })
    }

    if (this.data.newarray!==null&&this.data.newarray.length==2){
      //数组不添加
      console.log(111)
     
    }else{
      
      console.log(22)
      if (this.data.newarray == null || this.data.newarray == '') {
        //数组无数据，直接添加
        this.data.newarray.push(this.data.array[e.detail.value]);
        this.setData({
          newarray: this.data.newarray
        })

      }else{
        //循环判断是否重复添加
        for (var i = 0; i < this.data.newarray.length; i++) {
          if (this.data.newarray[i] == this.data.array[e.detail.value]) {
              //表示相同不需要添加
            }else{
              //表示不同可以添加操作            
              this.data.newarray.push(this.data.array[e.detail.value]);
              this.setData({
                newarray: this.data.newarray
              })
            }
        }
      }

    }
   
    console.log( this.data.newarray)
  },


//删除操作
  deletemotiontype0:function(e){
    for(var i=0;i<this.data.newarray.length;i++){
      if (this.data.newarray[i]=="有氧运动"){
        delete this.data.newarray[i]//删除第i项
        this.setData({
                newarray:this.data.newarray
            })
      }
    
    this.setData({
      yytypeshow: 3,
    })
    }
    console.log(this.data.newarray)
  },
  deletemotiontype1: function (e) {
    for (var i = 0; i < this.data.newarray.length; i++) {
      if (this.data.newarray[i] == "无氧运动") {
        delete this.data.newarray[i]//删除第i项
        this.setData({
          newarray: this.data.newarray
        })
      }
      this.setData({
        wytypeshow: 3,
      })

    }
    console.log(this.data.newarray)
    
  },


  // 转发
  onShareAppMessage: function () {
    return {
      title: '爱丁生活方式管理营',
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 调用函数时，传入new Date()参数，返回值是日期和时间  
    var time = util.formatTime(new Date());
    var date = util.formatday(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据  
    this.setData({
      time: time,
      date: date
    });  

    this.setData({
      type: options.type,
      id: options.id,
      dietId: options.dietId,
      sign:options.sign,
      times:options.times,
    })
    if (options.startdate != null && options.startdate != undefined) {
      //表示的跳转修改赋值操作
      this.setData({
        date: options.startdate,
        time: options.timestar,
        temperature: options.temperature,
        motiontype: options.motiontype,
        motionname: options.motionname,
        motiontime: options.motiontime,
        motionlongtime: options.motionlongtime
      })
    }

//改变导航头的名称
    if(this.data.type==11&this.data.times==1){
      wx.setNavigationBarTitle({
        title: '早间运动'
      })
    }
    if (this.data.type == 11 & this.data.times == 2) {
      wx.setNavigationBarTitle({
        title: '上午运动'
      })
    }
    if (this.data.type == 11 & this.data.times == 4) {
      wx.setNavigationBarTitle({
        title: '下午运动'
      })
    }
    if (this.data.type == 11 & this.data.times == 6) {
      wx.setNavigationBarTitle({
        title: '夜间运动'
      })
    }

    //改变导航头的名称
    if (this.data.type == 7 & this.data.times == 1) {
      wx.setNavigationBarTitle({
        title: '体温打卡'
      })
    }



  },

  //获取年月日
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })

  },
  //获取时分秒
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })

  },
  //获取温度
  inputtiwen: function (e) {
    this.setData({
      temperature: e.detail.value
    })
   
  },
 
  
  // 获取运动名称
  motionname:function(e){
    this.setData({
      motionname:e.detail.value
    })
  },

//获取运动时间
  motiontime:function(e){
    this.setData({
      motiontime: e.detail.value
    })
  },
  //获取持续时间
  motionlongtime:function(e){
    this.setData({
      motionlongtime: e.detail.value
    })
  },

  //添加操作
  submitadd: function () {
    var that = this;
    submitadd(that);

  },
  idsubmitadd: function () {
    var that = this;
    idsubmitadd(that);
  },

  //取消操作返回上一层
  cenceltem: function () {
    wx.navigateBack({
      delta: 1
    })
  }

})


// 点击添加执行函数
function submitadd(that) {

  if(that.data.type==7){
    var patientid = wx.getStorageSync('userInfo').patientid;

    var starttime = that.data.date + " " + that.data.time;
    console.log(starttime)


    var out = {
      starttime: starttime,
      temperature: that.data.temperature
    }
    var detail = JSON.stringify(out);

    var img = that.data.outimg;
    var params = {
      patientid: patientid,
      img: img || null,
      detail: detail,
      type: that.data.type,
      times: that.data.times,
    }
    if (that.data.date != '' && that.data.time != '' && that.data.temperature != '' && that.data.date != undefined && that.data.time != undefined && that.data.temperature != undefined) {

      app.https.post(that, '/IbabyWebService/camp/submitDiet', params, function (res) {

        var dietId = res.dietId
        that.setData({ dietId: dietId })

        wx.redirectTo({
          url: '../chartlook/chartlook?type=' + that.data.type + '&dietId=' + that.data.dietId + "&times=" + that.data.times+"&sign="+that.data.sign
        })
      })


    } else {
      wx.showModal({
        title: '提示',
        content: '请填写完整!',

      })
    }
  } else if (that.data.type == 11){
    var patientid = wx.getStorageSync('userInfo').patientid;
    if (that.data.newarray.length==2){//当newarray数组长度为2的时候，显示sports格式二种有氧，无氧分开传入
      for (var i = 0; i < that.data.newarray.length; i++) {
        if (that.data.newarray[i] == "无氧运动") {
          var wytype = that.data.newarray[i]
        } else {
          var yytype = that.data.newarray[i]
        }
      }
      console.log(wytype)
      var sportstype = [
        {
          type: wytype,
          sports: that.data.motionname,

        }, {
          type: yytype,
          time: that.data.motionlongtime,
        }
      ]
    } else if (that.data.newarray.length == 1){//当数组newarray数组长度为1的时候，判断运动类型，是有氧还是无氧，
      if(that.data.newarray[0]=="无氧运动"){
        var wytype = that.data.newarray[0];
        var sportstype = [
          {
            type: wytype,
            sports: that.data.motionname,

          }
        ]
      } else if (that.data.newarray[0] == "有氧运动"){
        var yytype = that.data.newarray[0];
        var sportstype = [
          {
            type: yytype,
            time: that.data.motionlongtime,

          }
        ]
      } 
    } else if (that.data.newarray.length == 0) {
      var sportstype = [
        {
          type: '',
          sports: '',
          time: '',
        }
      ]
    }
   

    var out={
      sportstype: sportstype,
      starttime: that.data.motiontime,
    }
    var detail = JSON.stringify(out);

    //添加按钮传入后台的参数
    var params = {
      patientid: patientid,
      img: img || null,
      detail: detail,
      type: that.data.type,
      times: that.data.times,
    }
    if (out.sportstype[0].type !== "" && out.sportstype[0].sports !== "" && out.sportstype[0].time !== "" && out.starttime !== "请输入运动开始时间") {
    
    //写接口
      app.https.post(that, '/IbabyWebService/camp/submitDiet', params, function (res) {

        var dietId = res.dietId
        that.setData({ dietId: dietId })
        wx.redirectTo({
          url: '../chartlook/chartlook?type=' + that.data.type + '&dietId=' + that.data.dietId + "&times=" + that.data.times+'&sign='+that.data.sign
        })
      })

  }else{
      wx.showModal({
        title: '提示',
        content: '请填写完整!',

      })
  }
 }
}

// 添加后的

function idsubmitadd(that) {

  if(that.data.type==7){
    var starttime = that.data.date + " " + that.data.time;

    var out = {
      starttime: starttime,
      temperature: that.data.temperature
    }

    var detail = JSON.stringify(out);

    var paramsadd = {
      detail: detail,
      id: that.data.dietId
    }
    if (that.data.date != '' && that.data.time != '' && that.data.temperature != '' && that.data.date != undefined && that.data.time != undefined && that.data.temperature != undefined) {

      app.https.post(that, '/IbabyWebService/camp/clockEdit', paramsadd, function (res) {

        console.log(res);
        wx.redirectTo({
          url: '../chartlook/chartlook?type=' + that.data.type + '&dietId=' + that.data.dietId + '&times=' + that.data.times+'&sign='+that.data.sign
        })
      })

    } else {
      wx.showModal({
        title: '提示',
        content: '请填写完整!',

      })
    }
  }else if(that.data.type==11){
    //数据接口

    if (that.data.newarray.length == 2) {//当newarray数组长度为2的时候，显示sports格式二种有氧，无氧分开传入
      for (var i = 0; i < that.data.newarray.length; i++) {
        if (that.data.newarray[i] == "无氧运动") {
          var wytype = that.data.newarray[i]
        } else {
          var yytype = that.data.newarray[i]
        }
      }
      console.log(wytype)
      var sportstype = [
        {
          type: wytype,
          sports: that.data.motionname,

        }, {
          type: yytype,
          time: that.data.motionlongtime,
        }
      ]
    } else if (that.data.newarray.length == 1) {//当数组newarray数组长度为1的时候，判断运动类型，是有氧还是无氧，
      if (that.data.newarray[0] == "无氧运动") {
        var wytype = that.data.newarray[0];
        var sportstype = [
          {
            type: wytype,
            sports: that.data.motionname,

          }
        ]
      } else if (that.data.newarray[0] == "有氧运动") {
        var yytype = that.data.newarray[0];
        var sportstype = [
          {
            type: yytype,
            time: that.data.motionlongtime,

          }
        ]
      }
    }  else if (that.data.newarray.length == 0) {
      var sportstype = [
        {
          type: '',
          sports: '',
          time: '',
        }
      ]
    }
    var out = {
      sportstype: sportstype,
      starttime: that.data.motiontime,
    }
    var detail = JSON.stringify(out);

    //传入后台的参数
    var paramsadd = {
      detail: detail,
      id: that.data.dietId
    }
    if (out.sportstype[0].type!=="" && out.sportstype[0].sports !=="" && out.sportstype[0].time !== "" && out.starttime !== "请输入运动开始时间") {

      //写接口
      app.https.post(that, '/IbabyWebService/camp/clockEdit', paramsadd, function (res) {

        wx.redirectTo({
          url: '../chartlook/chartlook?type=' + that.data.type + '&dietId=' + that.data.dietId + "&times=" + that.data.times+"&sign="+that.data.sign
        })
      })

    } else {
      wx.showModal({
        title: '提示',
        content: '请填写完整!',

      })
    }
  }

}











